package com.itheima.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Slf4j
@Aspect
public class TimeRecord {
    //在空方法上用注解@Pointcut,可以提高切入点表达式的复用性
        @Pointcut("execution(* com.itheima.service.*.*(..))")
        public void pt(){};
    //@Around("execution(* com.itheima.service.*.*(..))")
    @Around("pt()")
    public Object recprdTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //运行前执行
        Long begin=System.currentTimeMillis();
        //源程序
        Object result=proceedingJoinPoint.proceed();
            //获取目标类名
        String name = proceedingJoinPoint.getTarget().getClass().getName();
        log.info("目标类名:{}",name);
            //获取目标的方法名
        String name1 = proceedingJoinPoint.getSignature().getName();
        log.info("目标方法名:{}",name1);
            //获取方法参数
        Object[] args = proceedingJoinPoint.getArgs();
        log.info("目标参数:{}", Arrays.toString(args));
        //运行后执行
        Long end=System.currentTimeMillis();
        log.info(proceedingJoinPoint.getSignature()+"程序运行时间:{}ms",end-begin);
        return result;
    }
}
